package com.css.fxfzaqbz.modules.assess.service.impl;

import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.assess.entity.RiskAssessmentEntity;
import com.css.fxfzaqbz.modules.assess.repository.RiskAssessmentRepository;
import com.css.fxfzaqbz.modules.assess.service.RiskAssessmentService;
import com.css.fxfzaqbz.util.*;
import com.css.fxfzaqbz.web.PlatformSessionContext;
import com.css.fxfzaqbz.zcpt.sys.entity.SUser;
import com.css.fxfzaqbz.zcpt.sys.service.SUserService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author y1fan
 * @create 2022-09-08 18:56
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class RiskAssessmentServiceImpl implements RiskAssessmentService {

    @Resource
    private RiskAssessmentRepository riskAssessmentRepository;

    @Resource
    private SUserService sUserService;



    @Override
    public RiskAssessmentEntity saveRiskAssessment(RiskAssessmentEntity riskAssessmentEntity) {

        riskAssessmentEntity.setSraId(UUIDGenerator.getUUID());
        riskAssessmentEntity.setSraAssDate(PlatformDateUtils.getCurrentTimestamp());
        riskAssessmentEntity.setDelFlag(YNEnum.N.toString());
        riskAssessmentEntity.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        riskAssessmentEntity.setTypes("地震危险性评估");
        riskAssessmentEntity.setMark(YNEnum.N.toString());
        //如果标题为空  就设置成   "地震活动趋势分析";
        if (riskAssessmentEntity.getSraAssFileTitel().equals("")){
            riskAssessmentEntity.setSraAssFileTitel("地震活动趋势分析");
        }
        String userID = PlatformSessionContext.getUserID();
        riskAssessmentEntity.setCreateUser(userID);
        String userJson = sUserService.getSUser(userID);
        SUser sUser = CreateGson.createGson().fromJson(userJson, SUser.class);
        riskAssessmentEntity.setSraAssPeople(sUser.getUserName());
        return riskAssessmentRepository.saveRiskAssessment(riskAssessmentEntity);
    }



    @Override
    public List<RiskAssessmentEntity> getAllRiskAssessment(String id) {
        return riskAssessmentRepository.getAllRiskAssessment(id);
    }

    @Override
    public void deleteRiskAssessment(String id) {
        String userID = PlatformSessionContext.getUserID();
        Timestamp currentTimestamp = PlatformDateUtils.getCurrentTimestamp();
        riskAssessmentRepository.deleteRiskAssessment(id,userID,currentTimestamp);
    }

    @Override
    public RiskAssessmentEntity updateRiskAssessment(RiskAssessmentEntity riskAssessmentEntity) {
        String userID = PlatformSessionContext.getUserID();
        Timestamp currentTimestamp = PlatformDateUtils.getCurrentTimestamp();
        riskAssessmentEntity.setUpdateUser(userID);
        String userJson = sUserService.getSUser(userID);
        SUser sUser = CreateGson.createGson().fromJson(userJson, SUser.class);
        riskAssessmentEntity.setSraAssPeople(sUser.getUserName());
        riskAssessmentEntity.setUpdateTime(currentTimestamp);
        return riskAssessmentRepository.updateRiskAssessment(riskAssessmentEntity);
    }

    @Override
    public RiskAssessmentEntity getRiskAssessmentById(String id) {
        RiskAssessmentEntity riskAssessmentById = riskAssessmentRepository.getRiskAssessmentById(id);
        if (!PlatformObjectUtils.isEmpty(riskAssessmentById.getSraAssFileDescribe())){
            List<String> list = dataString(riskAssessmentById.getSraAssFileDescribe());
            riskAssessmentById.setDescribe(list);
        }
        return riskAssessmentById;

    }

    @Override
    public RiskAssessmentEntity getRiskAssessmentByContend(RiskAssessmentEntity riskAssessmentEntity) {
       return null;
    }

    @Override
    public RiskAssessmentEntity updateRiskAssessmentById(RiskAssessmentEntity riskAssessmentEntity, String sraId) {

        //riskAssessmentEntity.setSraId(sraId);
        riskAssessmentEntity.setSraAssDate(PlatformDateUtils.getCurrentTimestamp());
        String userID = PlatformSessionContext.getUserID();
        riskAssessmentEntity.setCreateUser(userID);
        riskAssessmentEntity.setUpdateTime(null);
        riskAssessmentEntity.setUpdateUser(null);
        riskAssessmentEntity.setCreateTime(PlatformDateUtils.getCurrentTimestamp());
        return riskAssessmentRepository.updateRiskAssessmentById(riskAssessmentEntity,sraId);
    }



    /*
    *
    * 查询最终成果
    * */
    @Override
    public List<RiskAssessmentEntity> getEarthEntityTwo(String id) {
        List<RiskAssessmentEntity> allRiskAssessment = riskAssessmentRepository.getEarthEntityTwo(id);
        for (RiskAssessmentEntity riskAssessmentEntity : allRiskAssessment) {
            if (!PlatformObjectUtils.isEmpty(riskAssessmentEntity.getSraAssFileDescribe())){
                //处理字符串转集合 "@@@"
                List<String> disList=dataString(riskAssessmentEntity.getSraAssFileDescribe());
                riskAssessmentEntity.setDescribe(disList);
            }
        }
        return allRiskAssessment;
    }

    @Override
    public void firstSubmitRiskAssessment(String id) {
        String userID = PlatformSessionContext.getUserID();
        Timestamp currentTimestamp = PlatformDateUtils.getCurrentTimestamp();
        RiskAssessmentEntity riskAssessmentById = riskAssessmentRepository.getRiskAssessmentById(id);
     List<RiskAssessmentEntity> riskAssessmentEntities= riskAssessmentRepository.getRiskAssessmentByContend(riskAssessmentById.getSraAssContent(), riskAssessmentById.getSraRelation());

        if (riskAssessmentEntities!=null){

            if (riskAssessmentEntities.get(0).getSraAssFileDescribe()!=""){
                riskAssessmentById.setSraAssFileDescribe(riskAssessmentEntities.get(0).getSraAssFileDescribe());
                riskAssessmentRepository.firstSubmitRiskAssessment(id,userID,currentTimestamp,riskAssessmentById.getSraAssFileDescribe());
            }else {
                riskAssessmentRepository.firstSubmitRiskAssessment2(id,userID,currentTimestamp);
            }
        }else {
            riskAssessmentRepository.firstSubmitRiskAssessment2(id,userID,currentTimestamp);
        }
    }

    @Override
    public Boolean riskAssessmentIfRepetition(RiskAssessmentEntity riskAssessmentEntity) {
        RiskAssessmentEntity  riskAssessmentEntity1=riskAssessmentRepository.riskAssessmentIfRepetition(riskAssessmentEntity);
        if (riskAssessmentEntity1==null){
            return false;
        }else {
            return true;
        }
    }

    @Override
    public void addRiskAssessmentDisaster(List<String> ids, String describe) {
        riskAssessmentRepository.addRiskAssessmentDisaster(ids,describe);
    }

    @Override
    public void removeRiskAssessmentDisasterAss(String id) {
        RiskAssessmentEntity riskAssessmentById = riskAssessmentRepository.getRiskAssessmentById(id);
        riskAssessmentById.setMark(YNEnum.N.toString());
        riskAssessmentById.setSraAssFileDescribe("");
        riskAssessmentById.setSubmiter("");
        riskAssessmentById.setSubmitPhone("");
        riskAssessmentRepository.removeRiskAssessmentDisasterAss(riskAssessmentById);
    }

    @Override
    public void secondSubmitRiskAssessment(List<String> ids) {
        String userJson = sUserService.getSUser(PlatformSessionUtils.getUserId());
        SUser sUser = CreateGson.createGson().fromJson(userJson, SUser.class);
        Timestamp currentTimestamp = PlatformDateUtils.getCurrentTimestamp();
        riskAssessmentRepository.secondSubmitRiskAssessment(ids,sUser,currentTimestamp);
    }

    @Override
    public int whatIsThisNumber(RiskAssessmentEntity riskAssessmentEntity) {
        RiskAssessmentEntity riskAssessmentEntity1=  riskAssessmentRepository.whatIsThisNumber(riskAssessmentEntity);
        if (PlatformObjectUtils.isEmpty(riskAssessmentEntity1)){
            return 0;
        }else if (riskAssessmentEntity1.getMark().equals(YNEnum.N.toString())){
            return 1;
        }else if (riskAssessmentEntity1.getMark().equals(YNEnum.Y.toString())){
            return 2;
        }
        return 2;
    }

    @Override
    public void coverRiskAssessment(RiskAssessmentEntity riskAssessmentEntity) {
        String userID = PlatformSessionContext.getUserID();
        Timestamp currentTimestamp = PlatformDateUtils.getCurrentTimestamp();
        String a="地震活动趋势分析";
        riskAssessmentRepository.deleteRiskAssessmentByTitel(a ,userID,currentTimestamp,riskAssessmentEntity.getSraRelation());
        saveRiskAssessment(riskAssessmentEntity);
    }

    @Override
    public List<RiskAssessmentEntity> getRiskByIaIdAndType(String iaId, String dzwxqpgType) {
       return riskAssessmentRepository.getRiskByIaIdAndType(iaId,dzwxqpgType);
    }

    /*
     * 字符串转集合方法
     * */
    private List<String> dataString(String sdaAssFileDescribe) {
        String[] split = sdaAssFileDescribe.split("@@@");
        List<String> disList=new ArrayList<>();
        Collections.addAll(disList,split);
        return disList;
    }

}
